import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { defineAsyncComponent } from "vue";
export const Layout = () => import("@/layout/index.vue");
/**
 * 上述代码示例中，我们通过 defineAsyncComponent 显式地定义了一个异步组件。
 * 这样做不仅可以确保组件按需加载，还能帮助 TypeScript 更好地识别 .vue 文件。
 * 如果问题仍然存在，可能需要进一步检查项目配置，
 * 比如 vue.config.js 和 tsconfig.json 文件中的设置。
 *
 */
// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/home/index.vue"),
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "HOME",
        component: () => import("@/views/home/index.vue"),
        meta: {
          title: "首页",
        },
      },
      {
        path: "/about",
        name: "ABOUTS",
        component: () => import("@/views/about/index.vue"),
        meta: {
          title: "关于",
        },
      },
      {
        path: "/serviceList",
        name: "SERVICE",
        component: () => import("@/views/serviceList/index.vue"),
        meta: {
          title: "服务",
        },
      },
      {
        path: "/news",
        name: "NEWS",
        component: () => import("@/views/news/index.vue"),
        meta: {
          title: "新闻",
        },
      },
      {
        path: "/case",
        name: "CASE",
        component: () => import("@/views/case/index.vue"),
        meta: {
          title: "案例",
        },
      },
      {
        path: "/connectUs",
        name: "CONNECT",

        component: () => import("@/views/connectUs/index.vue"),
        meta: {
          title: "联系我们",
        },
      },
    ],
  },

  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/test",
    name: "Test",
    component: defineAsyncComponent(() => import("@/views/test/index.vue")),
  },
  {
    path: "/testElementPlus",
    name: "TestElementPlus",
    component: defineAsyncComponent(
      () => import("@/views/testElementPlus/index.vue")
    ),
  },
  {
    path: "/echartsDemo",
    name: "EchartsDemo",
    component: defineAsyncComponent(
      () => import("@/views/echartDemo/index.vue")
    ),
  },
];

// 创建路由
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 重置路由

export function resetRouter() {
  router.replace({ path: "/login" });
}
export default router;
